/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gmail.avereshchaga.models;

import com.gmail.avereshchaga.transport.Client;
import com.gmail.avereshchaga.transport.entity.Invoice;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author <a href="mailto:avereshchaga@gmail.com">Vereshchaga Alexey</a>
 */
public class InvoiceTableModel extends AbstractTableModel {

    private String columnNames[] = {"ID", "Created at", "Created by"};
    private ArrayList<Invoice> invoices =
            (ArrayList<Invoice>) new Client().getInvoices();

    @Override
    public int getRowCount() {
        return invoices.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Invoice recordFromInvoice = invoices.get(rowIndex);

        switch (columnIndex) {
            case 0:
                return recordFromInvoice.getId();
            case 1:
                DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
                String dateStr = dateFormat.format(recordFromInvoice.getCreatedAt());
                return dateStr;
            case 2:
                return recordFromInvoice.getUser().getFirstName() + " "
                        + recordFromInvoice.getUser().getLastName();
            default:
                throw new UnsupportedOperationException("Not Supported Operation");
        }
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }
}
